﻿   using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections;
using System.Xml;


public partial class SJZD_NameManage : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initData();
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
        }
    }

    private void initData()
    {
        ZSql zs = new ZSql();

        string sql = "select * from (SELECT a.*,(SELECT COUNT(*)  FROM CS_Stock WHERE ModelId = a.SpecificationsId) AS Stock,(SELECT Units FROM Units WHERE UnitsId = a.Units) AS UnitsName,(SELECT ComoditiesName FROM ComoditiesName WHERE ComoditiesNameId = a.ComoditiesNameId) AS ComoditiesName,(SELECT Brand  FROM Brand  WHERE BrandId = a.BrandId) AS Brand,(SELECT ComoditiesType  FROM ComoditiesType   WHERE TypeId = a.TypeId) AS ComoditiesType FROM Specifications a) TableSpecifications  order by TypeId desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }

    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string SpecificationsId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddSpecifications.aspx?SpecificationsId=" + SpecificationsId + "&moduid=" + hfxs.Value + "&action=update");

    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string SpecificationsId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddSpecifications.aspx?SpecificationsId=" + SpecificationsId + "&moduid=" + hfxs.Value + "&action=flag");

    }

    protected void Linkprint_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + id + "')", true);

    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 12, GvData, btndel, btnallflag, btnAdd);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();
            if (AuditState == "1")
            {
                e.Row.Cells[10].Text = "已通过";
                e.Row.Cells[10].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "0")
            {
                e.Row.Cells[10].Text = "未通过";
                e.Row.Cells[10].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "")
            {
                e.Row.Cells[9].Text = "未审核";
                e.Row.Cells[9].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            //删除是否可用
            string delDisabled = DataBinder.Eval(e.Row.DataItem, "Stock").ToString();
            if (Convert.ToInt32(delDisabled) > 0)
            {
                (e.Row.Cells[12].FindControl("chkDelete") as CheckBox).Enabled = false;
            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)//------------------------添加-------------
    {
        Response.Redirect("AddSpecifications.aspx?moduid=" + hfxs.Value + "&action=add");
    }

    protected void btnallflag_Click(object sender, EventArgs e)//---------------------审核
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[10].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update Specifications set AuditState=" + flag + " where SpecificationsId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[11].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete Specifications  where SpecificationsId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvData.RenderControl(htw);
        string strHtml = sw.ToString().Trim();

        string ExcelFileName = "型号管理" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls";
        string FilePhysicialPathName = Request.PhysicalApplicationPath + "/xls";

        //生成的Excel文件名
        string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);

        if (File.Exists(objectExcelFileName))//如果有此文件,先删除
        {
            File.Delete(objectExcelFileName);
        }
        FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
        BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB2312"));
        Response.Charset = "GB2312";
        bw.Write(strHtml);
        bw.Close();
        fs.Close();
        Response.Redirect("../xls/型号管理" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void btnSelec_Click(object sender, EventArgs e)//搜索
    {
        string sql = "select * from (SELECT a.*,(SELECT COUNT(*)  FROM CS_Stock WHERE ModelId = a.SpecificationsId) AS Stock,(SELECT Units FROM Units WHERE UnitsId = a.Units) AS UnitsName,(SELECT ComoditiesName FROM ComoditiesName WHERE ComoditiesNameId = a.ComoditiesNameId) AS ComoditiesName,(SELECT Brand  FROM Brand  WHERE BrandId = a.BrandId) AS Brand,(SELECT ComoditiesType  FROM ComoditiesType   WHERE TypeId = a.TypeId) AS ComoditiesType FROM Specifications a) TableSpecifications  where 1=1 ";
        
        if (txtseletype.Text != "")
        {
            sql += " and ComoditiesType like'%" + Common.FormatParameter(txtseletype.Text) + "%'";
        }
        if (txtSpecifications.Text != "")
        {
            sql += " and Specifications like'%" + Common.FormatParameter(txtSpecifications.Text) + "%'";
        }

        if (txtselecName.Text != "")
        {
            sql += " and modulbh like'%" + Common.FormatParameter(txtselecName.Text) + "%'";
        }

        if (txtselename.Text != "")
        {
            sql += " and ComoditiesName like'%" + Common.FormatParameter(txtselename.Text) + "%' ";
        }

        if (txtseleband.Text != "")
        {
            sql += "  and Brand like'%" + Common.FormatParameter(txtseleband.Text) + "%' ";
        }

        sql += "  ORDER BY TypeId DESC";
        //Response.Write(sql);
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    
}
